<?php
/*
 * This script is responsinble for FB data real-time update.
 * If the user changes any of his/her user data,
 * then the FB calls this script in order to notify
 * that the locally stored user data are outdated.
 *
 * Since some of the user data (e.g. pic) can be accessed just with valid acceess token,
 * this script just marks the given user in the DB (tabel `user` column `update`).
 * The user data are updated only when the given user logs in,
 * and with the access_token the updated user data are fetched from FB.
 *
 * @see http://developers.facebook.com/docs/api/realtime
 */

header('Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');

date_default_timezone_set('UTC');

include_once './log4php/Logger.php';
include_once './Utils.php';
include_once './config.php';
include_once './MySQLConnect.php';

// initialize logging
Logger::configure('./log4j.properties');

$log = Logger::getLogger(basename(__FILE__));
$log->debug('[START] ' . basename(__FILE__));

// determine the request method
$method = $_SERVER['REQUEST_METHOD'];
$log->debug("Method: $method");

switch ($method) {
    case 'GET':
        foreach ($_GET as $key => $value) {
            $log->debug('$_GET[' . $key . '] = ' . $value);
        }


        $hub_mode = $_GET["hub_mode"];
        $hub_challenge = $_GET["hub_challenge"];
        $hub_verify_token = $_GET["hub_verify_token"];

        if ('subscribe' != $hub_mode) {
            $log->error("Unsupported mode: '$hub_mode'");
            break;
        }

        if (REALTIME_VERIFY_TOKEN == $hub_verify_token) {
            $log->info("Verify token match: '$hub_verify_token'");
            echo $hub_challenge;
            break;
        }
        else {
            $log->error("Invalid verify token: '$hub_verify_token'");
            break;
        }

        break;
    case 'POST':
        // get the post raw data
        $post_raw_data = file_get_contents('php://input');
        if (!$post_raw_data) {
            $log->error("Failed to get POST DATA!");
            break;
        }
        else {
            $log->debug("POST DATA: " . $post_raw_data);    
        }

        // 
        // {"object":"user","entry":[{"uid":"1781804022","id":"1781804022","time":1290941228,"changed_fields":["friends"]}]}
        // {"object":"user","entry":[{"uid":"100000024471694","id":"100000024471694","time":1290941228,"changed_fields":["friends"]}]}
        // {"object":"user","entry":[{"uid":"100000024471694","id":"100000024471694","time":1291057310,"changed_fields":["pic","pic_square"]}]}
        //

        $post_array = json_decode($post_raw_data, TRUE);
        $log->debug("POST array: " . Utils::toString($post_array));

        if ($post_array['object']!='user') {
            $log->error('The object field is not the expected value!');
            break;
        }

        if (!isset($post_array['entry']) || !is_array($post_array['entry'])) {
            $log->error('The entry field is not found!');
            break;
        }

        $uid_array = array();
        foreach ($post_array['entry'] as $entry) {
            $uid_array[] = $entry['uid'];
        }

        $log->debug("Users will be set as outdated: " . Utils::toString($uid_array));
        MySQLConnect::setUserAsOutdated($uid_array);

        break;
    default:
        $log->error("Unsupported request method: $method");
        break;
}

$log->debug('[ END ] ' . basename(__FILE__));

?>
